package com.hmall.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //执行前做一件事情
        System.out.println("执行MyGlobalFilter的pr方法");
        //放行，把处理后的结果交给下一过滤器进行操作
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            //执行后做一件事情
            System.out.println("执行MyGlobalFilter的过滤器post");
        }));
    }

    @Override
    public int getOrder() {
        //优先级，值越小，优先级越高
        return 0;
    }
}
